<?php
/**
 * [获取字符串的首字母]
 * 中文返回拼音首字母 英文返回首字母 其他返回"#"
 */
function getFirstChar($s0){
    $fchar = ord($s0{0});
    if($fchar >= ord("A") && $fchar <= ord("z") )return strtoupper($s0{0});
    $s1 = get_encoding($s0,'GB2312');
    $s2 = get_encoding($s1,'UTF-8');
    $s2 == $s0 ? $s = $s1 : $s = $s0;
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if($asc >= -20319 && $asc <= -20284) return "A";
    if($asc >= -20283 && $asc <= -19776) return "B";
    if($asc >= -19775 && $asc <= -19219) return "C";
    if($asc >= -19218 && $asc <= -18711) return "D";
    if($asc >= -18710 && $asc <= -18527) return "E";
    if($asc >= -18526 && $asc <= -18240) return "F";
    if($asc >= -18239 && $asc <= -17923) return "G";
    if($asc >= -17922 && $asc <= -17418) return "I";
    if($asc >= -17417 && $asc <= -16475) return "J";
    if($asc >= -16474 && $asc <= -16213) return "K";
    if($asc >= -16212 && $asc <= -15641) return "L";
    if($asc >= -15640 && $asc <= -15166) return "M";
    if($asc >= -15165 && $asc <= -14923) return "N";
    if($asc >= -14922 && $asc <= -14915) return "O";
    if($asc >= -14914 && $asc <= -14631) return "P";
    if($asc >= -14630 && $asc <= -14150) return "Q";
    if($asc >= -14149 && $asc <= -14091) return "R";
    if($asc >= -14090 && $asc <= -13319) return "S";
    if($asc >= -13318 && $asc <= -12839) return "T";
    if($asc >= -12838 && $asc <= -12557) return "W";
    if($asc >= -12556 && $asc <= -11848) return "X";
    if($asc >= -11847 && $asc <= -11056) return "Y";
    if($asc >= -11055 && $asc <= -10247) return "Z";
    return "#";
}
function get_encoding($data,$to){
    $encode_arr=['UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP'];
    $encoded=mb_detect_encoding($data, $encode_arr);
    $data = mb_convert_encoding($data,$to,$encoded);
    return $data;
}
/**
 * [创建聊天室的聊天内容文件 / 也可以实现清除聊天记录的功能]
 */
function creatChatFile($chatRoomId) {
	if (!is_dir(APP_PATH."../public/upload/chatfile/")) {
		mkdir(iconv("UTF-8", "GBK", APP_PATH."../public/upload/chatfile/"), 0777, true);
	}
	$chatfile_path = APP_PATH."../public/upload/chatfile/".$chatRoomId.".chatfile";
	$chatfile_path = iconv("UTF-8", "GBK", $chatfile_path);
	if(file_exists($chatfile_path)){
		@unlink($chatfile_path);
	}
	$chatfile = fopen($chatfile_path, "w");
	fclose($chatfile);
	$save_path = "/upload/chatfile/".$chatRoomId.".chatfile";
	return $save_path;
}

/**
 * [推送socket消息]
 * @param $uid_str = "1,2,3,4..."
 * @param $msg string
 * @param $userInfo = ['head'=>string,'real_name'=>string,'id'=>string,'msg_name'=>string]
 */
function send_socket_msg($uid_str,$msg,$msgInfo) {
	$uid_arr = explode(",",$uid_str);
	$bool = true;
	$error_ids = [];
	foreach($uid_arr as $v){
		$client = stream_socket_client('tcp://127.0.0.1:5678', $errno, $errmsg, 1);
		$data = ['uid'=>"uid:".$v, 'msg'=>$msg,'info'=>$msgInfo];
		fwrite($client, json_encode($data)."\n");
		$res = fread($client, 8192);
		if($res != "ok\n"){
			$bool = false;
			$error_ids[] = $v;
		}
	}
	if($bool){
		return ['code'=>1,'msg'=>'success'];
	}else{
		return ['code'=>0,'msg'=>'fail:'.implode(",",$error_ids)];
	}
}
/**
 * [消息编码]
 * @param $userInfo = ['head' => string,'nick_name' => string,'id'=> string]
 */
function socketMsg_encode($send_id="",$userInfo=[],$msg="") {
	$xml = '<msg class="user_id_'.$send_id.'">';
		$xml .='<img class="user-head" src="'.$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$userInfo['head'].'"/>';
		$xml .='<name>'.$userInfo['nick_name'].'</name>';
		$xml .='<time>'.date("Y-m-d H:i").'</time>';
		$xml .='<ct>'.$msg.'</ct>';
	$xml .='</msg>';
	return $xml;
}
?>
